<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/4/10
 * Time: 13:53
 */
namespace app\admin\controller;
use think\Db;
class Capital  extends Base {
    public function index(){
        if(request()->isAjax ()){
            $params = input('param.');
            $where= array();
            //日期
            if(isset($params['add_start']) && $params['add_start'] != '' && isset($params['add_end']) && $params['add_end'] != ''){
                $where['a.glod_time'] = ['between', [$params['add_start'], $params['add_end']]];
            }
            if(isset($params['add_start']) && $params['add_start'] == '' && isset($params['add_end']) && $params['add_end'] != ''){
                $where['a.glod_time'] = ['<', $params['add_end']];
            }
            if(isset($params['add_start']) && $params['add_start'] != '' && isset($params['add_end']) && $params['add_end'] == ''){
                $where['a.glod_time'] = ['>', $params['add_start']];
            }

            if(isset($params['glod_type']) && $params['glod_type'] != ''){
                $where['a.glod_type'] =$params['glod_type'] ;
            }

            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('glod')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.glod_user_id','left')
                ->where($where)->count();// 获取总条数
            $count2 = Db::name('glod')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.glod_user_id','left')
                ->where($where)->column('glod_id');// 获取总条数
            $lists = Db::name('glod')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.glod_user_id','left')
                ->page($Nowpage,$limits)
                ->where($where)
                ->field('a.*,uuu.user_username')
                ->order('glod_time desc')
                ->select();
            foreach ($lists as $key=>&$vo){
                if($vo['glod_status']==1){
                    $vo['glod_status_name'] = '签到';
                }elseif ($vo['glod_status']==2){
                    $vo['glod_status_name'] = '闯关';
                }elseif ($vo['glod_status']==3){
                    $vo['glod_status_name'] = '完善信息';
                }elseif ($vo['glod_status']==4){
                    $vo['glod_status_name'] = '段位晋升';
                }elseif ($vo['glod_status']==5){
                    $vo['glod_status_name'] = '购物';
                }elseif($vo['glod_status']==6){
                    $vo['glod_status_name'] = '知识对战';
                }
                if($vo['glod_type']==1){
                    $vo['glod_type_name'] ='收入';
                }elseif ($vo['glod_type']==2){
                    $vo['glod_type_name'] ='支出';
                }
            }
            $str = '';
            if($count2){
                $str = implode($count2,',');
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'count2' => $str, 'data' => $lists]);
        }
        $where1['glod_status']= 5;
        $glod_money = Db::table('up_glod')->where($where1)->sum('glod_money');
        $where2['glod_status']= array('IN',array(1,2,3,4,6));
        $glod_money1 = Db::table('up_glod')->where($where2)->sum('glod_money');
        $this->assign('glod_money',$glod_money);
        $this->assign('glod_money1',$glod_money1);
        return $this->fetch();
    }

    /***
     * @return \think\response\Json
     * 导出
     */
    public function excelIn(){
        $ids = input('param.ids');
        if($ids == ''){
            return json(['code' => 100, 'msg' => '请选择数据']);
        }
        $where['glod_id'] = array('IN',$ids);
        $lists = Db::name('glod')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.glod_user_id','left')
            ->field('a.*,uuu.user_username')
            ->where($where)
            ->order('user_status desc')
            ->select();
        foreach ($lists as $key=>&$vo){
            if($vo['glod_status']==1){
                $vo['glod_status_name'] = '签到';
            }elseif ($vo['glod_status']==2){
                $vo['glod_status_name'] = '闯关';
            }elseif ($vo['glod_status']==3){
                $vo['glod_status_name'] = '完善信息';
            }elseif ($vo['glod_status']==4){
                $vo['glod_status_name'] = '段位晋升';
            }elseif ($vo['glod_status']==5){
                $vo['glod_status_name'] = '购物';
            }elseif($vo['glod_status']==6){
                $vo['glod_status_name'] = '知识对战';
            }
            if($vo['glod_type']==1){
                $vo['glod_type_name'] ='收入';
            }elseif ($vo['glod_type']==2){
                $vo['glod_type_name'] ='支出';
            }
            unset($lists[$key]['glod_id']);
            unset($lists[$key]['glod_status']);
            unset($lists[$key]['glod_type']);
            unset($lists[$key]['glod_type']);
        }

        $cellname = [
            ['glod_time','流转时间',20,'LEFT'],
            ['glod_user_id','用户ID',15,'LEFT'],
            ['user_username','姓名',15,'LEFT'],
            ['glod_money','额度',15,'LEFT'],
            ['glod_status_name','流转类型',15,'LEFT'],
            ['glod_type_name','流转方式',15,'LEFT'],
        ];
        $res = exportExcel('平台资金流转列表','Qrcode-buildings',$cellname,$lists);
        return json($res);
    }
}